<?php
/**
 * 熵巡 - 企业级氚云OpenAPI数据管理平台
 * 开发归属 Alyr.net
 * 氚云API连接测试 - 测试与氚云服务器的基本连接
 * 尊重劳动成果是一种美德
 */

// 禁用错误输出防止HTML污染
error_reporting(0);
ini_set('display_errors', 0);

// 设置session目录权限
ini_set('session.save_path', sys_get_temp_dir());
session_start();

// 检查登录状态
if (!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] !== true) {
    header('Content-Type: application/json');
    echo json_encode(['success' => false, 'message' => '请先登录']);
    exit;
}

// 引入配置文件和客户端类
require_once '../config.php';
require_once '../classes/H3OpenApiClient.php';

header('Content-Type: application/json');

try {
    $config = require '../config.php';
    $client = new H3OpenApiClient($config);
    
    // 测试基本连接
    $url = $config['baseUrl'];
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    
    $result = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $error = curl_error($ch);
    curl_close($ch);
    
    if ($error) {
        echo json_encode([
            'success' => false, 
            'message' => '连接失败: ' . $error
        ]);
    } else {
        echo json_encode([
            'success' => true, 
            'message' => '连接成功', 
            'http_code' => $httpCode
        ]);
    }
} catch (Exception $e) {
    echo json_encode([
        'success' => false, 
        'message' => '连接测试失败: ' . $e->getMessage()
    ]);
}
?>